﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using Common.Model;
using Common.Error;
using OpenAPI.Model.OAuth;
using OpenAPI.Model.API;
using OpenAPI.BLL.OAuth;
using OpenAPI.Web.Mvc.Helper;
using OpenAPI.Web.Mvc.Filter;

namespace OpenAPI.Web.Mvc.Controllers
{
    [ExceptionFilter]
    [ManagerAuthFilter]
    public class ConsumerController : Controller
    {

        [HttpGet]
        public ActionResult Index()
        {
            return View();
        }


        #region Consumer Manage

        [HttpGet]
        public ActionResult ConsumerRegister()
        {
            IList<ConsumerLevelInfo> levels = OpenAPI.BLL.OAuth.ConsumerManager.GetConsumerLevels();
            ConsumerLevelInfo level = new ConsumerLevelInfo();
            level.CONSUMER_LEVEL = "";
            level.CONSUMER_LEVEL_NAME = "";
            level.CONSUMER_LEVEL_DESC = "";
            levels.Insert(0, level);

            ViewData["Levels"] = new SelectList(levels, "CONSUMER_LEVEL", "CONSUMER_LEVEL_NAME", "");

            return View();
        }

        [HttpGet]
        public ActionResult ConsumerList()
        {
            IList<ConsumerInfo> consumers = OpenAPI.BLL.OAuth.ConsumerManager.GetConsumers();
            ViewData["Consumers"] = consumers;

            return View();
        }

        [HttpGet]
        public ActionResult ConsumerModify(string id)
        {
            string oauth_consumer_key = id;
            ErrorType error;
            string oauth_consumer_secret = OpenAPI.BLL.OAuth.ConsumerManager.GetOAuthConsumerSecret(oauth_consumer_key, out error);

            ConsumerInfo consumer = ConsumerManager.GetConsumer(oauth_consumer_key, oauth_consumer_secret);

            IList<ConsumerLevelInfo> levels = OpenAPI.BLL.OAuth.ConsumerManager.GetConsumerLevels();
            ConsumerLevelInfo level = new ConsumerLevelInfo();
            level.CONSUMER_LEVEL = "";
            level.CONSUMER_LEVEL_NAME = "";
            level.CONSUMER_LEVEL_DESC = "";
            levels.Insert(0, level);

            ViewData["Levels"] = new SelectList(levels, "CONSUMER_LEVEL", "CONSUMER_LEVEL_NAME", consumer.CONSUMER_LEVEL);

            return View(consumer);
        }

        [HttpPost]
        [ActionName("ConsumerRegister")]
        public ActionResult AjaxConsumerRegister()
        {
            ResultInfo result = new ResultInfo();

            bool result_stat = false;
            ErrorType error = ErrorHelper.NewError("", "Register Consumer Failure");

            ConsumerInfo consumer = new ConsumerInfo();
            consumer.OAUTH_CONSUMER_KEY = APIHelper.CreateOAuthToken();
            consumer.OAUTH_CONSUMER_SECRET = APIHelper.CreateOAuthTokenSecret();
            consumer.CONSUMER_REQUEST_URL = Utility.StringHelper.GetPost("consumer_request");
            consumer.CONSUMER_CALLBACK_URL = Utility.StringHelper.GetPost("consumer_callback");
            consumer.CONSUMER_STATUS = 1;
            consumer.CONSUMER_NAME = Utility.StringHelper.GetPost("consumer_name");
            consumer.CONSUMER_LEVEL = Utility.StringHelper.GetPost("consumer_level");
            consumer.CONSUMER_EMAIL = Utility.StringHelper.GetPost("consumer_email");

            if (string.IsNullOrEmpty(consumer.OAUTH_CONSUMER_KEY))
            {
                result.FLAG = false;
                result.CODE = "";
                result.DESC = "Create Consumer Key Failure";
                result.DATA = null;
            }
            else if (string.IsNullOrEmpty(consumer.OAUTH_CONSUMER_SECRET))
            {
                result.FLAG = false;
                result.CODE = "";
                result.DESC = "Create Consumer Secret Failure";
                result.DATA = null;
            }
            else if (string.IsNullOrEmpty(consumer.CONSUMER_NAME))
            {
                result.FLAG = false;
                result.CODE = "";
                result.DESC = "Consumer Name Can't Empty";
                result.DATA = null;
            }
            else if (string.IsNullOrEmpty(consumer.CONSUMER_REQUEST_URL))
            {
                result.FLAG = false;
                result.CODE = "";
                result.DESC = "Request URL Can't Empty";
                result.DATA = null;
            }
            else if (string.IsNullOrEmpty(consumer.CONSUMER_CALLBACK_URL))
            {
                result.FLAG = false;
                result.CODE = "";
                result.DESC = "Callback URL Can't Empty";
                result.DATA = null;
            }
            else if (string.IsNullOrEmpty(consumer.CONSUMER_LEVEL))
            {
                result.FLAG = false;
                result.CODE = "";
                result.DESC = "Consumer Level Can't Empty";
                result.DATA = null;
            }
            else
            {
                result_stat = OpenAPI.BLL.OAuth.ConsumerManager.ConsumerRegister(consumer, out error);
                if (result_stat)
                {
                    result.FLAG = true;
                    result.CODE = "";
                    result.DESC = "Success";
                    result.DATA = null;
                }
                else
                {
                    result.FLAG = false;
                    result.CODE = error.ERROR_CODE;
                    result.DESC = error.ERROR_MESSAGE;
                    result.DATA = null;
                }
            }

            return Json(result, JsonRequestBehavior.AllowGet);
        }


        [HttpPost]
        [ActionName("ConsumerModify")]
        public ActionResult AjaxConsumerModify()
        {
            ResultInfo result = new ResultInfo();

            bool result_stat = false;
            ErrorType error = ErrorHelper.NewError("", "Modify Failure");
            ConsumerInfo consumer = new ConsumerInfo();

            consumer.OAUTH_CONSUMER_KEY = Utility.StringHelper.GetPost("oauth_consumer_key");
            consumer.OAUTH_CONSUMER_SECRET = Utility.StringHelper.GetPost("oauth_consumer_secret");

            consumer.CONSUMER_REQUEST_URL = Utility.StringHelper.GetPost("consumer_request");
            consumer.CONSUMER_CALLBACK_URL = Utility.StringHelper.GetPost("consumer_callback");
            consumer.CONSUMER_NAME = Utility.StringHelper.GetPost("consumer_name");
            consumer.CONSUMER_LEVEL = Utility.StringHelper.GetPost("consumer_level");
            consumer.CONSUMER_EMAIL = Utility.StringHelper.GetPost("consumer_email");

            if (string.IsNullOrEmpty(consumer.OAUTH_CONSUMER_KEY))
            {
                result.FLAG = false;
                result.CODE = "";
                result.DESC = "Consumer Key Can't Empty";
                result.DATA = null;
            }
            else if (string.IsNullOrEmpty(consumer.OAUTH_CONSUMER_SECRET))
            {
                result.FLAG = false;
                result.CODE = "";
                result.DESC = "Consumer Secret Can't Empty";
                result.DATA = null;
            }
            else if (string.IsNullOrEmpty(consumer.CONSUMER_NAME))
            {
                result.FLAG = false;
                result.CODE = "";
                result.DESC = "Consumer Name Can't Empty";
                result.DATA = null;
            }
            else if (string.IsNullOrEmpty(consumer.CONSUMER_REQUEST_URL))
            {
                result.FLAG = false;
                result.CODE = "";
                result.DESC = "Request URL Can't Empty";
                result.DATA = null;
            }
            else if (string.IsNullOrEmpty(consumer.CONSUMER_CALLBACK_URL))
            {
                result.FLAG = false;
                result.CODE = "";
                result.DESC = "Callback URL Can't Empty";
                result.DATA = null;
            }
            else if (string.IsNullOrEmpty(consumer.CONSUMER_LEVEL))
            {
                result.FLAG = false;
                result.CODE = "";
                result.DESC = "Consumer Level Can't Empty";
                result.DATA = null;
            }
            else
            {
                result_stat = OpenAPI.BLL.OAuth.ConsumerManager.ConsumerModify(consumer, out error);
                if (result_stat)
                {
                    result.FLAG = true;
                    result.DESC = "Success";
                    result.DATA = null;
                }
                else
                {
                    result.FLAG = false;
                    result.CODE = error.ERROR_CODE;
                    result.DESC = error.ERROR_MESSAGE;
                    result.DATA = null;
                }
            }

            return Json(result, JsonRequestBehavior.AllowGet);
        }



        [HttpPost]
        [ActionName("DeleteConsumer")]
        public ActionResult AjaxDeleteConsumer()
        {
            ResultInfo result = new ResultInfo();

            bool result_stat = false;
            ErrorType error = ErrorHelper.NewError("", "Delete Failure");
            string oauth_consumer_key = Utility.StringHelper.GetPost("oauth_consumer_key");
            string oauth_consumer_secret = Utility.StringHelper.GetPost("oauth_consumer_secret");

            if (string.IsNullOrEmpty(oauth_consumer_key))
            {
                result.FLAG = false;
                result.CODE = "";
                result.DESC = "Consumer Key Can't Empty";
                result.DATA = null;
            }
            else if (string.IsNullOrEmpty(oauth_consumer_secret))
            {
                result.FLAG = false;
                result.CODE = "";
                result.DESC = "Consumer Secret Can't Empty";
                result.DATA = null;
            }
            else
            {
                result_stat = OpenAPI.BLL.OAuth.ConsumerManager.DeleteConsumer(oauth_consumer_key, oauth_consumer_secret, out error);
                if (result_stat)
                {
                    result.FLAG = true;
                    result.DESC = "Success";
                    result.DATA = null;
                }
                else
                {
                    result.FLAG = false;
                    result.CODE = error.ERROR_CODE;
                    result.DESC = error.ERROR_MESSAGE;
                    result.DATA = null;
                }
            }

            return Json(result, JsonRequestBehavior.AllowGet);
        }



        #endregion

        #region Consumer Level

        [HttpGet]
        public ActionResult ConsumerLevelList()
        {
            IList<ConsumerLevelInfo> consumerLevels = OpenAPI.BLL.OAuth.ConsumerManager.GetConsumerLevels();
            ViewData["ConsumerLevels"] = consumerLevels;

            return View();
        }

        [HttpGet]
        public ActionResult ConsumerLevelAdd()
        {
            return View();
        }

        [HttpGet]
        public ActionResult ConsumerLevelModify(string id)
        {
            string consumer_level = id;
            ConsumerLevelInfo level = OpenAPI.BLL.OAuth.ConsumerManager.GetConsumerLevel(consumer_level);

            return View(level);
        }

        [HttpPost]
        public ActionResult AjaxConsumerLevelAdd()
        {
            ResultInfo result = new ResultInfo();

            bool result_stat = false;
            ErrorType error = ErrorHelper.NewError("", "Add Consumer Level Failure");

            ConsumerLevelInfo level = new ConsumerLevelInfo();
            level.CONSUMER_LEVEL = Utility.StringHelper.GetPost("consumer_level").Trim();
            level.CONSUMER_LEVEL_NAME = Utility.StringHelper.GetPost("consumer_level_name").Trim();
            level.CONSUMER_LEVEL_DESC = Utility.StringHelper.GetPost("consumer_level_desc");

            if (string.IsNullOrEmpty(level.CONSUMER_LEVEL))
            {
                result.FLAG = false;
                result.CODE = "";
                result.DESC = "Consumer Level Can't Empty";
                result.DATA = null;
            }
            else if (string.IsNullOrEmpty(level.CONSUMER_LEVEL_NAME))
            {
                result.FLAG = false;
                result.CODE = "";
                result.DESC = "Consumer Level Name Can't Empty";
                result.DATA = null;
            }
            else
            {
                result_stat = OpenAPI.BLL.OAuth.ConsumerManager.InsertConsumerLevel(level, out error);
                if (result_stat)
                {
                    result.FLAG = true;
                    result.CODE = "";
                    result.DESC = "Add Consumer Level Success";
                    result.DATA = null;
                }
                else
                {
                    result.FLAG = false;
                    result.CODE = error.ERROR_CODE;
                    result.DESC = error.ERROR_MESSAGE;
                    result.DATA = null;
                }
            }

            return Json(result, JsonRequestBehavior.AllowGet);
        }


        [HttpPost]
        public ActionResult AjaxConsumerLevelModify()
        {
            ResultInfo result = new ResultInfo();

            bool result_stat = false;
            ErrorType error = ErrorHelper.NewError("", "Modify Consumer Level Failure");

            ConsumerLevelInfo level = new ConsumerLevelInfo();
            level.CONSUMER_LEVEL = Utility.StringHelper.GetPost("consumer_level").Trim();
            level.CONSUMER_LEVEL_NAME = Utility.StringHelper.GetPost("consumer_level_name").Trim();
            level.CONSUMER_LEVEL_DESC = Utility.StringHelper.GetPost("consumer_level_desc");

            if (string.IsNullOrEmpty(level.CONSUMER_LEVEL))
            {
                result.FLAG = false;
                result.CODE = "";
                result.DESC = "Consumer Level Can't Empty";
                result.DATA = null;
            }
            else if (string.IsNullOrEmpty(level.CONSUMER_LEVEL_NAME))
            {
                result.FLAG = false;
                result.CODE = "";
                result.DESC = "Consumer Level Name Can't Empty";
                result.DATA = null;
            }
            else
            {
                result_stat = OpenAPI.BLL.OAuth.ConsumerManager.UpdateConsumerLevel(level, out error);
                if (result_stat)
                {
                    result.FLAG = true;
                    result.CODE = "";
                    result.DESC = "Modify Consumer Level Success";
                    result.DATA = null;
                }
                else
                {
                    result.FLAG = false;
                    result.CODE = error.ERROR_CODE;
                    result.DESC = error.ERROR_MESSAGE;
                    result.DATA = null;
                }
            }

            return Json(result, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public ActionResult AjaxDeleteConsumerLevel()
        {
            ResultInfo result = new ResultInfo();

            bool result_stat = false;
            ErrorType error = ErrorHelper.NewError("", "Delete Consumer Level Failure");

            string consumer_level = Utility.StringHelper.GetPost("consumer_level").Trim();

            if (string.IsNullOrEmpty(consumer_level))
            {
                result.FLAG = false;
                result.CODE = "";
                result.DESC = "Consumer Level Can't Empty";
                result.DATA = null;
            }
            else
            {
                result_stat = OpenAPI.BLL.OAuth.ConsumerManager.DeleteConsumerLevel(consumer_level, out error);
                if (result_stat)
                {
                    result.FLAG = true;
                    result.CODE = "";
                    result.DESC = "Delete Consumer Level Success";
                    result.DATA = null;
                }
                else
                {
                    result.FLAG = false;
                    result.CODE = error.ERROR_CODE;
                    result.DESC = error.ERROR_MESSAGE;
                    result.DATA = null;
                }
            }

            return Json(result, JsonRequestBehavior.AllowGet);
        }


        [ManagerAuthFilter]
        [HttpGet]
        public ActionResult Pop_MethodInLevel(string id)
        {
            string consumer_level = id;
            ConsumerLevelInfo level = OpenAPI.BLL.OAuth.ConsumerManager.GetConsumerLevel(consumer_level);

            IList<MethodInfo> methods = OpenAPI.BLL.API.MethodManager.GetMethods("");
            ViewData["Methods"] = methods;

            IList<MethodInConsumerLevelInfo> selectedMethods = OpenAPI.BLL.OAuth.ConsumerManager.GetMethodsInConsumerLevel(consumer_level);
            ViewData["SelectedMethods"] = selectedMethods;

            return View(level);
        }

        [ManagerAuthFilter]
        [HttpPost]
        public ActionResult AjaxSaveMethodsToLevel()
        {
            ResultInfo result = new ResultInfo();

            ErrorType error = null;
            bool result_stat = false;

            string consumer_level = Utility.StringHelper.GetPost("consumer_level").Trim();
            string[] arr_methods = Utility.StringHelper.GetPost("str_methods").Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

            if (string.IsNullOrEmpty(consumer_level))
            {
                result_stat = false;
                error = ErrorHelper.NewError("", "Consumer level can not empty.");
            }
            else
            {
                result_stat = OpenAPI.BLL.OAuth.ConsumerManager.SaveMethodsToConsumerLevel(consumer_level, arr_methods, out error);

                if (result_stat)
                {
                    error = null;
                }
            }

            //处理返回结果
            if (result_stat)
            {
                result.FLAG = true;
                result.CODE = "";
                result.DESC = "Success";
                result.DATA = null;
            }
            else
            {
                result.FLAG = false;
                result.CODE = (error == null) ? "" : error.ERROR_CODE;
                result.DESC = (error == null) ? "" : error.ERROR_MESSAGE;
                result.DATA = null;
            }

            return Json(result, JsonRequestBehavior.AllowGet);
        }



        #endregion


    }//
}//
